package com.zyg.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zyg.model.UserInfo;
import com.zyg.util.UserInfoUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 拦截sql执行语句，自动添加值
 */
@Component
public class MybatisObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        setFieldValByName("createTime", new Date(),metaObject);
        setFieldValByName("isDeleted", false ,metaObject);

        UserInfo userInfo = UserInfoUtil.getUserSaveInfo();
        if(userInfo!=null && userInfo.getUserId()!=null){
            setFieldValByName("createUserId", userInfo.getUserId() ,metaObject);
            setFieldValByName("createUserName", userInfo.getUserName() ,metaObject);
            setFieldValByName("lesseeId", userInfo.getLesseeId() ,metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime",new Date(),metaObject);

        UserInfo userInfo = UserInfoUtil.getUserSaveInfo();
        if(userInfo!=null && userInfo.getUserId()!=null){
            setFieldValByName("updateUserId", userInfo.getUserId() ,metaObject);
            setFieldValByName("updateUserName", userInfo.getUserName() ,metaObject);
        }

    }

}